package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.bo.*;
import com.imooc.mapper.*;
import com.imooc.pojo.*;
import com.imooc.service.MqLocalMsgRecordService;
import com.imooc.service.ResumeService;
import com.imooc.utils.PageUtils;
import com.imooc.utils.PagedGridResult;
import com.imooc.vo.ResumeVO;
import com.imooc.vo.SearchResumesVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liansen
 * @create 02-24-15:03
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {

    @Autowired
    private MqLocalMsgRecordService recordService;

    @Autowired
    private ResumeWorkExpMapper resumeWorkExpMapper;

    @Autowired
    private ResumeProjectExpMapper resumeProjectExpMapper;

    @Autowired
    private ResumeEducationMapper resumeEducationMapper;

    @Autowired
    private ResumeExpectMapper resumeExpectMapper;

    @Autowired
    private ResumeMapperCustom resumeMapperCustom;

    @Override
    public void initResume(String userId) {
        Resume resume = new Resume();
        resume.setUserId(userId);
        resume.setCreateTime(LocalDateTime.now());
        resume.setUpdatedTime(LocalDateTime.now());
        baseMapper.insert(resume);
//        int i= 10 / 0;
    }

    @Override
    @Transactional
    public void initResume(String userId, String msgId) {
        Resume resume = new Resume();
        resume.setUserId(userId);
        resume.setCreateTime(LocalDateTime.now());
        resume.setUpdatedTime(LocalDateTime.now());
        baseMapper.insert(resume);
//        int i = 10 / 0;
        //按照消息id删除本地消息记录
        recordService.removeById(msgId);
    }

    @Override
    public void editResume(EditResumeBO editResumeBO) {
        Resume resume = new Resume();
        BeanUtils.copyProperties(editResumeBO, resume);
        LambdaQueryWrapper<Resume> wrapper = new LambdaQueryWrapper<Resume>()
                .eq(Resume::getId, editResumeBO.getId())
                .eq(Resume::getUserId, editResumeBO.getUserId());
        baseMapper.update(resume, wrapper);
    }

    @Override
    public ResumeVO getResume(String userId) {
        Resume resume = baseMapper.selectOne(new LambdaQueryWrapper<Resume>().eq(Resume::getUserId, userId));
        ResumeVO resumeVO = new ResumeVO();
        //如果不判断null的话, 如果resumeVO为null, 那么下面拷贝的时候会报错
        if(resume == null){
            return null;
        }
        BeanUtils.copyProperties(resume, resumeVO);
        //查询工作经验, 这里不做多表关联查询, 单独查表后进行组装, 避免高并发对数据库的压力
        List<ResumeWorkExp> resumeWorkExps = resumeWorkExpMapper
                .selectList(new LambdaQueryWrapper<ResumeWorkExp>().eq(ResumeWorkExp::getUserId, userId));
        //查询项目经验
        List<ResumeProjectExp> resumeProjectExps = resumeProjectExpMapper
                .selectList(new LambdaQueryWrapper<ResumeProjectExp>().eq(ResumeProjectExp::getUserId, userId));
        //查询教育经历
        List<ResumeEducation> resumeEducations = resumeEducationMapper
                .selectList(new LambdaQueryWrapper<ResumeEducation>().eq(ResumeEducation::getUserId, userId));

        resumeVO.setWorkExpList(resumeWorkExps);
        resumeVO.setProjectExpList(resumeProjectExps);
        resumeVO.setEducationList(resumeEducations);
        return resumeVO;
    }

    @Override
    public void editResumeWork(EditWorkExpBO editWorkExpBO) {
        ResumeWorkExp resumeWorkExp = new ResumeWorkExp();
        BeanUtils.copyProperties(editWorkExpBO, resumeWorkExp);
        resumeWorkExp.setUpdatedTime(LocalDateTime.now());
        if(StringUtils.isBlank(editWorkExpBO.getId())){
            //新增
            resumeWorkExp.setCreateTime(LocalDateTime.now());
            resumeWorkExpMapper.insert(resumeWorkExp);
        }else {
            //更新
            LambdaQueryWrapper<ResumeWorkExp> wrapper = new LambdaQueryWrapper<ResumeWorkExp>()
                                        .eq(ResumeWorkExp::getId, editWorkExpBO.getId())
                                        .eq(ResumeWorkExp::getUserId, editWorkExpBO.getUserId())
                                        .eq(ResumeWorkExp::getResumeId, editWorkExpBO.getResumeId());
            resumeWorkExpMapper.update(resumeWorkExp, wrapper);
        }
    }

    @Override
    public ResumeWorkExp getWorkExp(String workExpId, String userId) {
        LambdaQueryWrapper<ResumeWorkExp> wrapper = new LambdaQueryWrapper<ResumeWorkExp>()
                                        .eq(ResumeWorkExp::getId, workExpId)
                                        .eq(ResumeWorkExp::getUserId, userId);
        return resumeWorkExpMapper.selectOne(wrapper);
    }

    @Override
    public void deleteWorkExp(String workExpId, String userId) {
        LambdaQueryWrapper<ResumeWorkExp> wrapper = new LambdaQueryWrapper<ResumeWorkExp>()
                .eq(ResumeWorkExp::getId, workExpId)
                .eq(ResumeWorkExp::getUserId, userId);
        resumeWorkExpMapper.delete(wrapper);
    }

    @Override
    public void editProjectExp(EditProjectExpBO editProjectExpBO) {
        ResumeProjectExp projectExp = new ResumeProjectExp();
        BeanUtils.copyProperties(editProjectExpBO, projectExp);
        projectExp.setCreateTime(LocalDateTime.now());
        if(StringUtils.isBlank(editProjectExpBO.getId())){
            //新增
            projectExp.setUpdatedTime(LocalDateTime.now());
            resumeProjectExpMapper.insert(projectExp);
        }else {
            //修改
            LambdaQueryWrapper<ResumeProjectExp> wrapper = new LambdaQueryWrapper<ResumeProjectExp>()
                    .eq(ResumeProjectExp::getId, editProjectExpBO.getId())
                    .eq(ResumeProjectExp::getUserId, editProjectExpBO.getUserId())
                    .eq(ResumeProjectExp::getResumeId, editProjectExpBO.getResumeId());
            resumeProjectExpMapper.update(projectExp, wrapper);
        }
    }

    @Override
    public ResumeProjectExp getProjectExp(String projectExpId, String userId) {
        LambdaQueryWrapper<ResumeProjectExp> wrapper = new LambdaQueryWrapper<ResumeProjectExp>().eq(ResumeProjectExp::getId, projectExpId)
                .eq(ResumeProjectExp::getUserId, userId);
        return resumeProjectExpMapper.selectOne(wrapper);

    }

    @Override
    public void deleteProjectExp(String projectExpId, String userId) {
        LambdaQueryWrapper<ResumeProjectExp> wrapper = new LambdaQueryWrapper<ResumeProjectExp>().eq(ResumeProjectExp::getId, projectExpId)
                .eq(ResumeProjectExp::getUserId, userId);
        resumeProjectExpMapper.delete(wrapper);
    }

    @Override
    public void editEducation(EditEducationBO editEducationBO) {
        ResumeEducation resumeEducation = new ResumeEducation();
        BeanUtils.copyProperties(editEducationBO, resumeEducation);
        resumeEducation.setCreateTime(LocalDateTime.now());
        if(StringUtils.isBlank(editEducationBO.getId())){
            resumeEducation.setUpdatedTime(LocalDateTime.now());
            resumeEducationMapper.insert(resumeEducation);
        }else {
            LambdaQueryWrapper<ResumeEducation> wrapper = new LambdaQueryWrapper<ResumeEducation>().eq(ResumeEducation::getId, editEducationBO.getId())
                    .eq(ResumeEducation::getUserId, editEducationBO.getUserId())
                    .eq(ResumeEducation::getResumeId, editEducationBO.getResumeId());
            resumeEducationMapper.update(resumeEducation, wrapper);
        }
    }

    @Override
    public ResumeEducation getEducation(String eduId, String userId) {
        LambdaQueryWrapper<ResumeEducation> wrapper = new LambdaQueryWrapper<ResumeEducation>().eq(ResumeEducation::getId, eduId)
                .eq(ResumeEducation::getUserId, userId);
        return resumeEducationMapper.selectOne(wrapper);
    }

    @Override
    public void deleteEducation(String eduId, String userId) {
        LambdaQueryWrapper<ResumeEducation> wrapper = new LambdaQueryWrapper<ResumeEducation>().eq(ResumeEducation::getId, eduId)
                .eq(ResumeEducation::getUserId, userId);
        resumeEducationMapper.delete(wrapper);
    }

    @Override
    public void editJobExpect(EditResumeExpectBO editResumeExpectBO) {
        ResumeExpect resumeExpect = new ResumeExpect();
        BeanUtils.copyProperties(editResumeExpectBO, resumeExpect);
        resumeExpect.setCreateTime(LocalDateTime.now());
        if(StringUtils.isBlank(editResumeExpectBO.getId())){
            //新增
            resumeExpect.setUpdatedTime(LocalDateTime.now());
            resumeExpectMapper.insert(resumeExpect);
        }else {
            //编辑
            LambdaQueryWrapper<ResumeExpect> wrapper = new LambdaQueryWrapper<ResumeExpect>().eq(ResumeExpect::getId, editResumeExpectBO.getId())
                    .eq(ResumeExpect::getUserId, editResumeExpectBO.getUserId())
                    .eq(ResumeExpect::getResumeId, editResumeExpectBO.getResumeId());
            resumeExpectMapper.update(resumeExpect, wrapper);
        }
    }

    @Override
    public List<ResumeExpect> getMyResumeExpectList(String resumeId, String userId) {
        LambdaQueryWrapper<ResumeExpect> wrapper = new LambdaQueryWrapper<ResumeExpect>().eq(ResumeExpect::getResumeId, resumeId)
                .eq(ResumeExpect::getUserId, userId);
        return resumeExpectMapper.selectList(wrapper);
    }

    @Override
    public void deleteMyResumeExpect(String resumeExpectId, String userId) {
        LambdaQueryWrapper<ResumeExpect> wrapper = new LambdaQueryWrapper<ResumeExpect>().eq(ResumeExpect::getId, resumeExpectId)
                .eq(ResumeExpect::getUserId, userId);
        resumeExpectMapper.delete(wrapper);
    }

    @Override
    public PagedGridResult searchResumes(SearchResumesBO searchResumesBO, Integer page, Integer limit) {

        String basicTitle = searchResumesBO.getBasicTitle();
        String jobType = searchResumesBO.getJobType();
        Integer beginAge = searchResumesBO.getBeginAge();
        Integer endAge = searchResumesBO.getEndAge();
        Integer sex = searchResumesBO.getSex();
        Integer activeTimes = searchResumesBO.getActiveTimes();
        Integer beginWorkExpYears = searchResumesBO.getBeginWorkExpYears();
        Integer endWorkExpYears = searchResumesBO.getEndWorkExpYears();
        String edu = searchResumesBO.getEdu();
        List<String> eduList = searchResumesBO.getEduList();
        Integer beginSalary = searchResumesBO.getBeginSalary();
        Integer endSalary = searchResumesBO.getEndSalary();
        String jobStatus = searchResumesBO.getJobStatus();

        Map<String, Object> map = new HashMap<>();
        map.put("basicTitle", basicTitle);
        map.put("jobType", jobType);
        map.put("beginAge", beginAge);
        map.put("endAge", endAge);
        map.put("sex", sex);
        map.put("activeTimes", activeTimes);
        map.put("beginWorkExpYears", beginWorkExpYears);
        map.put("endWorkExpYears", endWorkExpYears);
        map.put("edu", edu);
        map.put("eduList", eduList);
        map.put("beginSalary", beginSalary);
        map.put("endSalary", endSalary);
        map.put("jobStatus", jobStatus);

        List<SearchResumesVO> list = resumeMapperCustom.searchResumesList(map);

        return PageUtils.setterPagedGrid(list, page);
    }


}
